export function FormTableMixin(service, defaultProps = {}) {
  const pagination = defaultProps && {
    pageNo: 1,
    pageSize: 15,
    total: 0,
    ...defaultProps,
  }

  return {
    data() {
      return {
        form: {
          activeIdx: 0,
          activeValue: null,
          value: null,
          onSubmit: () => {
            this.table.pagination.pageNo = 1
            this.getList()
          },
          onInput: val => {
            this.form.value = val
          },
          onSwitchType: (index, val) => {
            this.form.activeIdx = index
            this.form.activeValue = val
          },
        },
        table: {
          data: [],
          pagination,
          onChange: pagination => {
            Object.assign(this.table.pagination, pagination)
            this.getList()
          },
        },
      }
    },
    mounted() {
      this.getList()
    },
    computed: {
      params() {
        const { pagination } = this.table

        return {
          ...this.form.value,
          productType: this.form.activeValue,
          current: pagination.pageNo,
          limit: pagination.pageSize,
        }
      },
    },
    methods: {
      async getList() {
        const { data = [], total = 0 } = (await service.call(this, this.params, this)) || {}

        this.table.data = data

        if (this.table.pagination) {
          this.table.pagination.total = total
        }
      },
    },
  }
}
